﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Xml.Linq;

namespace IMDBAPI
{
    public class ImdbAPI
    {

        private const String NAME_SEARCH_URL = "http://www.omdbapi.com/?r=XML&plot=full&t={0}";
        private const String NAME_SEARCH_WY_URL = "http://www.omdbapi.com/?r=XML&plot=full&t={0}&y={1}";
        private const String NAME_LIST_SEARCH_URL = "http://www.omdbapi.com/?r=XML&plot=full&s={0}&y={1}";


        private const String ID_SEARCH_URL = "http://www.imdbapi.com/?r=XML&plot=full&i={0}";

        public ImdbMovie GetMovieByName(String movieName)
        {
            WebClient webClient = new WebClient();

            XmlSerializer s = new XmlSerializer(typeof(ImdbMovieResult));

            byte[] data = webClient.DownloadData(string.Format(NAME_SEARCH_URL, movieName));
            String xml = System.Text.Encoding.UTF8.GetString(data);
            //Console.WriteLine(xml);
            TextReader r = new StringReader(xml);
            ImdbMovieResult movie = (ImdbMovieResult)s.Deserialize(r);

            return movie.Movie;
        }

        public ImdbMovie GetMovieByName(String movieName, String year)
        {
            WebClient webClient = new WebClient();

            XmlSerializer s = new XmlSerializer(typeof(ImdbMovieResult));

            byte[] data = webClient.DownloadData(string.Format(NAME_SEARCH_WY_URL, movieName, year));
            String xml = System.Text.Encoding.UTF8.GetString(data);
            Console.WriteLine(xml);
            TextReader r = new StringReader(xml);
            ImdbMovieResult movie = (ImdbMovieResult)s.Deserialize(r);

            return movie.Movie;
        }

        public ImdbMovie GetMovieById(String movieId)
        {
            WebClient webClient = new WebClient();
            XmlSerializer s = new XmlSerializer(typeof(ImdbMovieResult));
            byte[] data = webClient.DownloadData(string.Format(ID_SEARCH_URL, movieId));
            String xml = System.Text.Encoding.UTF8.GetString(data);
            Console.WriteLine(xml);
            TextReader r = new StringReader(xml);
            ImdbMovieResult movie = (ImdbMovieResult)s.Deserialize(r);

            return movie.Movie;
        }

        public ImdbMovieSearchResults SearchMovieByName(String movieName, String year)
        {
            WebClient webClient = new WebClient();

            XmlSerializer s = new XmlSerializer(typeof(ImdbSearchItem[]),new XmlRootAttribute("root"));

            byte[] data = webClient.DownloadData(string.Format(NAME_LIST_SEARCH_URL, movieName, year));
            String xml = System.Text.Encoding.UTF8.GetString(data);
            Console.WriteLine(xml);
            TextReader r = new StringReader(xml);
            ImdbSearchItem[] movies = (ImdbSearchItem[])s.Deserialize(r);

            ImdbMovieSearchResults mov = new ImdbMovieSearchResults();
            mov.Movies = movies;

            return mov;
        }


    }
}
